<template>
  <!-- BoxGeometry可以用来创建三维立方体，可以指定立方体的宽度、高度、深度以及这三个方向上的分段数 -->
  <div class="box-geometry">
    <div id="scene"></div>
    <div class="controls-box">
      <section>
        <el-row>
          <div v-for="(item, key) in properties" :key="key">
            <div>
              <el-col :span="8">
                <span class="vertice-span">{{ item.name }}</span>
              </el-col>
              <el-col :span="13">
                <el-slider
                  v-model="item.value"
                  :min="item.min"
                  :max="item.max"
                  :step="item.step"
                ></el-slider>
              </el-col>
              <el-col :span="3">
                <span class="vertice-span">{{ item.value }}</span>
              </el-col>
            </div>
          </div>
        </el-row>
      </section>
    </div>
  </div>
</template>

<script>
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
import * as SceneUtils from "three/examples/jsm/utils/SceneUtils.js";

let scene;
let camera;
let renderer;
let controls;
export default {
  data() {
    return {
      properties: {
        width: {
          name: "width",
          value: 10,
          min: 0,
          max: 40,
          step: 1,
        },
        height: {
          name: "height",
          value: 10,
          min: 0,
          max: 40,
          step: 1,
        },
        depth: {
          name: "depth",
          value: 10,
          min: 0,
          max: 40,
          step: 1,
        },
        widthSegments: {
          name: "widthSeg",
          value: 4,
          min: 1,
          max: 20,
          step: 1,
        },
        heightSegments: {
          name: "heightSeg",
          value: 4,
          min: 1,
          max: 20,
          step: 1,
        },
        depthSegments: {
          name: "depthSeg",
          value: 4,
          min: 1,
          max: 20,
          step: 1,
        },
      },
      mesh: null,
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    // 初始化
    init() {
      this.createScene(); // 创建场景
      this.createMesh(); // 创建网格模型
      this.createLight(); // 创建光源
      this.createCamera(); // 创建相机
      this.createRender(); // 创建渲染器
      this.createControls(); // 创建控件对象
      this.render(); // 渲染
    },
    // 创建场景
    createScene() {
      scene = new THREE.Scene();
    },
    // 创建网格模型
    createMesh() {
      //创建立方体
      //BoxGeometry(width : Float, height : Float, depth : Float, widthSegments : Integer, heightSegments : Integer, depthSegments : Integer)
      //width — X轴上面的宽度，默认值为1。height — Y轴上面的高度，默认值为1。depth — Z轴上面的深度，默认值为1。
      //widthSegments — （可选）宽度的分段数，默认值是1。heightSegments — （可选）高度的分段数，默认值是1。depthSegments — （可选）深度的分段数，默认值是1。
      const geom = new THREE.BoxGeometry(
        this.properties.width.value,
        this.properties.height.value,
        this.properties.depth.value,
        Math.round(this.properties.widthSegments.value),
        Math.round(this.properties.heightSegments.value),
        Math.round(this.properties.depthSegments.value)
      );
      // 创建材质
      const meshMaterial = new THREE.MeshPhongMaterial({
        side: THREE.DoubleSide,
        color: 0x5de9ff,
      });
      const wireFrameMat = new THREE.MeshBasicMaterial({ wireframe: true });

      // 添加组合材质
      this.mesh = SceneUtils.createMultiMaterialObject(geom, [
        meshMaterial,
        wireFrameMat,
      ]);

      // 网格对象添加到场景中
      scene.add(this.mesh);
    },

    // 创建光源
    createLight() {
      const ambientLight = new THREE.AmbientLight(0xffffff, 0.1); // 创建环境光
      scene.add(ambientLight); // 将环境光添加到场景

      const spotLight = new THREE.SpotLight(0xffffff); // 创建聚光灯
      spotLight.position.set(-40, 60, -10);
      spotLight.castShadow = true;
      scene.add(spotLight);
    },
    // 创建相机
    createCamera() {
      const element = document.getElementById("scene");
      const k = element.clientWidth / element.clientHeight; // 窗口宽高比
      // PerspectiveCamera( fov, aspect, near, far )
      camera = new THREE.PerspectiveCamera(35, k, 0.1, 1000);
      camera.position.set(-80, 60, 40); // 设置相机位置

      camera.lookAt(new THREE.Vector3(10, 0, 0)); // 设置相机方向
      scene.add(camera);
    },
    // 创建渲染器
    createRender() {
      const element = document.getElementById("scene");
      renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
      renderer.setSize(element.clientWidth, element.clientHeight); // 设置渲染区域尺寸
      renderer.shadowMap.enabled = true; // 显示阴影
      renderer.shadowMap.type = THREE.PCFSoftShadowMap;
      renderer.setClearColor(0x3f3f3f, 1); // 设置背景颜色
      element.appendChild(renderer.domElement);
    },

    // 更新属性
    updateFun() {
      scene.remove(this.mesh);
      this.createMesh();
    },
    render() {
      this.updateFun();
      renderer.render(scene, camera);
      requestAnimationFrame(this.render);
    },
    // 创建控件对象
    createControls() {
      controls = new OrbitControls(camera, renderer.domElement);
    },
  },
};
</script>
<style>
.box-geometry {
  width: 100%;
  height: 100%;
  position: relative;
}
#scene {
  width: 100%;
  height: 100%;
}
.controls-box {
  position: absolute;
  right: 5px;
  top: 5px;
  width: 300px;
  padding: 10px;
  background-color: #fff;
  border: 1px solid #c3c3c3;
}
.vertice-span {
  line-height: 38px;
  padding: 0 2px 0 10px;
}
</style>

